﻿using SnmpForm.SnmpWrapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace SnmpForm.ApBatchConfig
{
    public class BatchTask
    {
        FrmApBatchConfig _objForm = null;
        List<BusinessWrapper.Ap> _aps = null;
        public BatchTask(FrmApBatchConfig objForm, 
            List<BusinessWrapper.Ap> aps)
        {
            _objForm = objForm;
            _aps = aps;
        }

        public void UpdateStatus(BusinessWrapper.Ap ap, string strStatus)
        {
            _objForm.BeginInvoke(_objForm.updateStatus,
                    new Object[] { ap, strStatus }
                    );
        }

        public virtual void ApRequestAction(BusinessWrapper.Ap ap)
        {
        }

        public virtual string OnApRequestSucess()
        {
            string strStatus = "成功";
            return strStatus;
        }

        public virtual void ApAction(BusinessWrapper.Ap ap)
        {
            string strStatus = "";
            try
            {
                ApRequestAction(ap);
                strStatus = OnApRequestSucess();
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                strStatus = "同步超时";
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                strStatus = "连接异常";
            }
            catch (SnmpErrorException ex)
            {
                strStatus = ("请求错误" + ex.Message);
            }
            catch (ApSnmpException ex)
            {
                strStatus = ex.Message;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                strStatus = "异常:" + ex.Message;
            }

            UpdateStatus(ap, strStatus);
        }

        public virtual void Run()
        {
            foreach (BusinessWrapper.Ap ap in _aps)
            {
                ApAction(ap);
            }
        }
    }
}
